package com.qg.cyapp.activity;

import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.qg.cyapp.R;
import com.qg.cyapp.bean.Order;
import com.qg.cyapp.util.ShareUtil;

public class WatchOrdersActivity extends HttpActivity {

	private LinearLayout llContent;
	private LayoutInflater inflater;
	private int startId = 0;
	private int number = 6;
	public static List<Order> orders;
	private TextView tvReturn;

	@Override
	public void afterPost(String result, int requestCode,
			Map<String, String> request) {
		// TODO Auto-generated method stub
		bean = getGsonBean(result);
		if (bean.state == ShareUtil.SUCCESS) {
			orders = gson.fromJson(bean.json, new TypeToken<List<Order>>() {
			}.getType());
			for (int i = 0; i < orders.size(); i++) {
				addOrder(orders.get(i), i);
			}
		}
	}

	@Override
	public int getLayoutId() {
		// TODO Auto-generated method stub
		return R.layout.activity_watch_order;
	}

	@Override
	public void setUI() {
		// TODO Auto-generated method stub
		inflater = LayoutInflater.from(this);

		llContent = (LinearLayout) findViewById(R.id.llContent);
		tvReturn = (TextView) mfindViewAndSetOnClick(R.id.tvReturn);
		
		getOrders();
	}

	private void addOrder(Order order, int position) {
		Log.v(TAG, "addOrder");
		View view = inflater.inflate(R.layout.item_order, null);
		TextView tvId, tvMealTimeName, tvSum, tvTime;
		
		mFindViewAndSetOnClick(view);
		view.setTag(position);
		
		tvId = (TextView) view.findViewById(R.id.tvOrderId);
		tvMealTimeName = (TextView) view.findViewById(R.id.tvMealTimeName);
		tvTime = (TextView) view.findViewById(R.id.tvTime);
		tvSum = (TextView) view.findViewById(R.id.tvSumPrice);

		tvId.setText(order.id);
		tvMealTimeName.setText(order.mealTimesName);
		tvSum.setText(String.valueOf(order.sum) + "元");
		tvTime.setText(order.time);
		llContent.addView(view);
	}

	public void getOrders() {
		params.clear();
		params.put("oldersNo", ShareUtil.olderId);
		params.put("startId", String.valueOf(startId));
		params.put("number", String.valueOf(number));
		params.put("action", "viewOrder");
		addPostRequest("OrderAction", params, 0);
		sendPostRequest();
	}

	@Override
	public void OnClickListener(View view) {
		// TODO Auto-generated method stub
		switch(view.getId()){
		case R.id.llOrder:
			Intent intent = new Intent(WatchOrdersActivity.this, OrderDetailActivity.class);
			intent.putExtra(ShareUtil.POSITION_KEY, (Integer)view.getTag());
			startActivity(intent);
			break;
		case R.id.tvReturn:
			finish();
			break;
		}
	}

}
